package view;

import data.Product;

import java.util.*;

import java.util.Scanner;


public class ShoppingCart extends Product {
    private static final List<String> purchaseHistory = new ArrayList<>();// 用于存储购买历史的列表
    // 向购物车中添加商品
    public static void addProduct(String productId2, int quantity, ArrayList<Product> list3, ArrayList<Product> cart) {
        boolean flag = false;
        for (int i = 0; i < list3.size(); i++) {
            Product pro = list3.get(i);
            if (Objects.equals(pro.getProductId(), productId2)) {
                cart.add(pro);
                for (int j = 0; j < cart.size(); j++) {
                    Product car = cart.get(j);
                    if (Objects.equals(car.getProductId(), productId2)) {
                        car.setCartQuantity(quantity);
                        flag = true;
                    }
                }
            }
            if (!flag) {
                System.out.println("无该商品编号");
            }
        }
    }
    // 从购物车中移除商品
    public static void removeProduct(String productId1, ArrayList<Product> cart) {
        System.out.println("你确定要删除商品编号为 " + productId1 + "的商品吗 ？(yes or no)");
        Scanner scanner = new Scanner(System.in);
        String response = scanner.nextLine();
        if ("yes".equals(response)) {
            for (int i = 0; i < cart.size(); i++) {
                Product pro = cart.get(i);
                if (Objects.equals(productId1, pro.getProductId())) {
                    cart.remove(pro);
                }

            }
            System.out.println("商品已成功移除。");
        } else {
            System.out.println("移除操作取消！");
        }
    }
    // 查看购物车中的商品
    public static void viewCart(ArrayList<Product> cart) {

        System.out.println("你的购物车有：");
        System.out.printf("%-10s %-15s %-10s %-15s %-15s %-8s %-8s %-5s%n",
                "商品编号", "商品名称", "生产厂家", "生产日期", "型号", "进货价", "零售价格", "购物车数量");
        for (Product car : cart) {
            System.out.printf("%-10s %-15s %-15s %-15s %-20s %-10.2f %-10.2f %-10d%n",
                    car.getProductId(),
                    car.getProductName(),
                    car.getManufacturer(),
                    car.getPorductionDate(),
                    car.getMdoel(),
                    car.getPurchasePrice(),
                    car.getRetailPrice(),
                    car.getCartQuantity());
        }


    }

    // 修改购物车中的商品数量
    public static void modifyProduct(String Id, ArrayList<Product> cart) {
        System.out.println("你确定要修改商品编号为 " + Id + " 的购物车数量吗？(yes or no)");
        Scanner scanner = new Scanner(System.in);
        String response = scanner.nextLine();
        if ("yes".equals(response)) {
            System.out.println("请输入新的数量");
            int quantity = scanner.nextInt();

            if (quantity > 0) {
                for (int i = 0; i < cart.size(); i++) {
                    Product car1 = cart.get(i);
                    if (Objects.equals(car1.getProductId(), Id)) {
                        System.out.println("该商品你的购物车有：");
                        car1.setCartQuantity(quantity);
                        System.out.printf("%-10s %-15s %-10s %-15s %-15s %-8s %-8s %-5s%n",
                                "商品编号", "商品名称", "生产厂家", "生产日期", "型号", "进货价", "零售价格", "购物车数量");
                        System.out.printf("%-10s %-15s %-15s %-15s %-20s %-10.2f %-10.2f %-10d%n",
                                car1.getProductId(),
                                car1.getProductName(),
                                car1.getManufacturer(),
                                car1.getPorductionDate(),
                                car1.getMdoel(),
                                car1.getPurchasePrice(),
                                car1.getRetailPrice(),
                                car1.getCartQuantity());
                    }
                }
            } else {
                for (int i = 0; i < cart.size(); i++) {
                    Product car1 = cart.get(i);
                    if (Objects.equals(car1.getProductId(), Id)) {
                        cart.remove(car1);
                    }
                }
            }
        }
    }
    // 结账
    public static void checkout(String payment, ArrayList<Product> list3, ArrayList<Product> cart, ArrayList<Product> items) {
        if (cart.isEmpty()) {
            System.out.println("购物车为空，无法结账");
        }
        switch (payment) {
            case "支付宝":
                System.out.println("支付宝支付成功！");

                for (int j = 0; j < list3.size(); j++) {
                    Product pro = list3.get(j);
                    for (int i = 0; i < cart.size(); i++) {
                        Product car1 = cart.get(i);

                        if (Objects.equals(pro.getProductId(), car1.getProductId())) {
                            items.add(car1);
                            int newquantity = pro.getQuantity() - car1.getCartQuantity();
                            pro.setQuantity(newquantity);
                            System.out.println("商品新的信息");
                            System.out.println("商品编号\t\t商品名称\t生产厂家\t生产日期\t型号\t进货价\t零售价格\t商品剩余数量");
                            System.out.println(STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");

                        }
                    }
                }
                purchaseHistory.add(new Date().toString());
                cart.clear();
                break;
            case "微信":
                System.out.println("微信支付成功！");
                for (int j = 0; j < list3.size(); j++) {
                    Product pro = list3.get(j);
                    for (int i = 0; i < cart.size(); i++) {
                        Product car1 = cart.get(i);
                        if (pro.getProductId() == car1.getProductId()) {
                            items.add(car1);
                            int newquantity = pro.getQuantity() - car1.getCartQuantity();
                            pro.setQuantity(newquantity);
                            System.out.println("商品新的信息");
                            System.out.println("商品编号\t\t商品名称\t生产厂家\t生产日期\t型号\t进货价\t零售价格\t购物车数量");
                            System.out.println(STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");
                        }
                    }
                }
                purchaseHistory.add(new Date().toString());
                cart.clear();
                break;
            case "银行卡":
                System.out.println("银行卡支付成功！");
                for (Product pro : list3) {
                    for (Product car1 : cart) {
                        if (pro.getProductId() == car1.getProductId()) {
                            items.add(car1);
                            int newquantity = pro.getQuantity() - car1.getCartQuantity();
                            pro.setQuantity(newquantity);
                            System.out.println("商品新的信息");
                            System.out.println("商品编号\t\t商品名称\t生产厂家\t生产日期\t型号\t进货价\t零售价格\t购物车数量");
                            System.out.println(STR."\{pro.getProductId()}\t\{pro.getProductName()}\t\{pro.getManufacturer()}\t\{pro.getPorductionDate()}\t\{pro.getMdoel()}\t\{pro.getPurchasePrice()}\t\{pro.getRetailPrice()}\t\{pro.getQuantity()}");

                        }
                    }
                }
                purchaseHistory.add(new Date().toString());
                cart.clear();
                break;
            default: {
                System.out.println("不支持的支付方法");
            }


        }
    }
    // 查看购物历史
    public static void viewPurchaseHistory(ArrayList<Product> item) {
        System.out.println("购物历史：");
        for (int i = 0; i < purchaseHistory.size(); i++) {
            System.out.println("第" + (i + 1) + "次购买时间" + purchaseHistory.get(i));
            System.out.println("购买的商品清单：");
            for (int j = 0; j < item.size(); j++) {
                Product pro = item.get(j);
                System.out.printf("%-10s %-15s %-10s %-15s %-15s %-8s %-5s%n",
                        "商品编号", "商品名称", "生产厂家", "生产日期", "型号", "零售价格", "购物车数量");
                System.out.printf("%-10s %-15s %-15s %-15s %-20s %-10.2f %-10d%n",
                        pro.getProductId(),
                        pro.getProductName(),
                        pro.getManufacturer(),
                        pro.getPorductionDate(),
                        pro.getMdoel(),
                        pro.getRetailPrice(),
                        pro.getQuantity());
            }
            System.out.println();
        }
    }
}



